package com.example.demo;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends Activity {
	private String IP = "192.168.1.100";
	private Integer Port = 12345;
	private EditText UserName;
	private EditText PassWord;
	private Button Login;
	private CheckBox Remember;
	protected Myapplication app;
	private ProgressDialog waitting;
	private Socket sc;
	private Protocol protocol = new Protocol();

	private TextView textStatus;
	private static final String DATAUSER="DATAUSER";
	private static final String USERNAME="USERNAME";
	private static final String PASSWORD="PASSWORD";
	private static final String REMEMBER="REMEMBER";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		UserName = (EditText) findViewById(R.id.userName);
		PassWord = (EditText) findViewById(R.id.passWord);
		Login = (Button) findViewById(R.id.ButtonLogin);
		app = (Myapplication) getApplication();

		textStatus= (TextView) findViewById(R.id.textView2);
		Remember = (CheckBox) findViewById(R.id.checkBox1);
		loadConfig();

		Login.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				// connect to server pc
                textStatus.setText("");
                saveConfig();
				String UserNameString = UserName.getText().toString();
				String PassWordString = PassWord.getText().toString();
			///	waitting= ProgressDialog.show(MainActivity.this, "", "Waitting..");
				try {
					
					sc = new Socket(IP, Port);
					
					app.setSocket(sc);

					protocol.sendMessages(sc, "", Protocol.USER_CONNECT);
					protocol.sendMessages(sc, UserNameString,
							Protocol.USER_NAME);
					protocol.sendMessages(sc, PassWordString,
							Protocol.USER_PASSWORD);
					String ack= protocol.Receive(sc);
					if (protocol.findTypeMsg(ack) == Protocol.USER_LOGIN_ACK && protocol.extractData(ack).equals("OK")== true){
						Bundle sendBundle = new Bundle();
                        sendBundle.putString("UserName", UserNameString);
						Intent i = new Intent(MainActivity.this, SelectDivice.class);
						Toast toast = Toast.makeText(getApplicationContext(),
								"Login is successfull", Toast.LENGTH_LONG);
						toast.show();
						i.putExtras(sendBundle);
						startActivity(i);
						
					}
					else
						textStatus.setText("UserName or password is incorrect!");
					/// waitting.dismiss();

				} catch (NumberFormatException e) {

				} catch (UnknownHostException e) {

				} catch (IOException e) {

				} 
			}
		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	/*
	 * public void run() { try { sc = new Socket(IP, Port); app.setSocket(sc); }
	 * catch (UnknownHostException e) { // TODO Auto-generated catch block
	 * e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated
	 * catch block e.printStackTrace(); }
	 * 
	 * }
	 */
	  void saveConfig()
	    {
	    	
	    	String text1 = this.UserName.getEditableText().toString();
	    	String text2 = this.PassWord.getEditableText().toString();
	    	SharedPreferences editPreference = this.
	    	getSharedPreferences(this.DATAUSER, Activity.MODE_PRIVATE);
	    	Editor editor = editPreference.edit();
	    	if(this.Remember.isChecked()==true){
		    	editor.putString(this.USERNAME, text1);
		    	editor.putString(this.PASSWORD, text2);
		    	editor.putBoolean(this.REMEMBER, true);
	    	}
	    	else{
	    		
	    		editor.putString(this.USERNAME,"");
		    	editor.putString(this.PASSWORD, "");
		    	editor.putBoolean(this.REMEMBER, false);
	    	}
		    	editor.commit();
	    }
	    
	    void loadConfig()
	    {
	    	SharedPreferences editPreference = this.
	    	getSharedPreferences(this.DATAUSER, Activity.MODE_PRIVATE);
	    	String text1= editPreference.getString(this.USERNAME, "");
	    	String text2= editPreference.getString(this.PASSWORD, "");
	    	this.Remember.setChecked(editPreference.getBoolean(this.REMEMBER,false));
	    	this.UserName.setText(text1);
	    	this.PassWord.setText(text2);
	    	

	    	
	    }

}
